package com.bjsxt.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.UserWallet;
import com.bjsxt.model.R;
import com.bjsxt.service.UserWalletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@Api(tags = "用户的提币地址")
@RequestMapping("/userWallets")
public class UserWalletController {


    @Autowired
    private UserWalletService userWalletService ;

    @GetMapping
    @ApiOperation(value = "分页查询用户的提币地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId" ,value = "用户的id") ,
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('user_wallet_query')")
    public R<Page<UserWallet>> findByPage(@ApiIgnore Page<UserWallet> page , Long userId){
        page.addOrder(OrderItem.desc("last_update_time")) ;
        Page<UserWallet> userWalletPage = userWalletService.findByPage(page ,userId) ;
        return R.ok(userWalletPage) ;
    }


    @GetMapping("/getCoinAddress/{coinId}")
    @ApiOperation(value = "货币地址查询")
    public R<List<UserWallet>> getCoinAddress(@PathVariable("coinId")Long coinId){

        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        List<UserWallet>userWallets = userWalletService.findUserWallers(userId,coinId);
        return R.ok(userWallets);

    }
    @PostMapping
    public R save(@RequestBody UserWallet userWallet){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        userWallet.setUserId(userId);
        boolean save = userWalletService.save(userWallet);
        if(save){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/deleteAddress")
    public R delete(@RequestParam(required = true) Long addressId,@RequestParam(required = true) String payPassword){

       boolean isOk=  userWalletService.deleteWallet(addressId,payPassword);

       if(isOk){
           return R.ok();
       }else {
           return R.fail();
       }

    }


}
